package com.example.test_touch.test

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.ViewGroup

/**
 * @author ytx
 * @date 2025-09-27 08:58
 * @description
 */
class MyViewGroup:ViewGroup{
    companion object{
        const val TAG = "MyViewGroup"
    }
    constructor(context: Context):this(context,null)
    constructor(context: Context,attributeSet: AttributeSet?):this(context,attributeSet,0)
    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr:Int):super(context,attributeSet,defStyleAttr)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        measureChildren(widthMeasureSpec,heightMeasureSpec) //测量子控件的大小
    }
    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        //摆放子控件的位置
        val child = getChildAt(0)
        child.layout(0,0,child.measuredWidth,child.measuredHeight)
    }


    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (ev?.action == MotionEvent.ACTION_DOWN){
            Log.d(TAG, "dispatchTouchEvent:分发 ")
        }
        return false
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        if (ev?.action == MotionEvent.ACTION_DOWN){
            Log.d(TAG, "onInterceptTouchEvent: 拦截")
        }
        return super.onInterceptTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (event?.action == MotionEvent.ACTION_DOWN){
            Log.d(TAG, "onTouchEvent: 消费")
        }
        return super.onTouchEvent(event)
    }

}